<template>
  <div>
    <div class="flexrowbetween flexwrapper" style="align-items: flex-start">
      <div class="flex">
        <div
          class="iconbox flexcenter commhover mr_10"
          @click="resetQuery"
        >
          <i class="el-icon-refresh f-20" :class="{ rotate: isrefresh }"></i>
        </div>
        <el-button plain type="primary" size="mini" @click="handleAdd">添加</el-button>
        <!-- <el-button plain type="danger" :disabled='multiple' size="mini" @click="handleDelete">删除</el-button> -->
      </div>
      <div>
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="0"
        >
          <el-form-item prop="keyword">
            <el-input
              class="searchinput"
              v-model="queryParams.keyword"
              placeholder="请输入用户名或订单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item class="mr_0">
            <el-dropdown trigger="click">
              <el-button size="mini" class="nor_border plr_9">
                <svg-icon class-name="f-14" icon-class="listicon" />
                <i class="el-icon-caret-bottom el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <div class="dropbody">
                  <el-checkbox-group
                    v-model="checkedOption"
                    class="flexcolumn"
                    @change="changeSelect"
                  >
                    <el-checkbox
                      v-for="(item, index) in selectoptions"
                      :label="item.label"
                      :key="index"
                      class="m_5"
                      >{{ item.label }}</el-checkbox
                    >
                  </el-checkbox-group>
                </div>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" class="nol_border" :loading='exportloading' @click="handleExport">
              <svg-icon class-name="f-14" icon-class="export" />
            </el-button>
            <!-- <el-dropdown trigger="click" @command="handleCommand">
              <el-button size="mini" class="nol_border plr_9">
                <svg-icon class-name="f-14" icon-class="export" />
                <i class="el-icon-caret-bottom el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  v-for="(item, index) in exportlist"
                  :key="index"
                  :command="item.id"
                  >{{ item.name }}</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown> -->
          </el-form-item>
          <el-form-item>
            <el-button
              class="f-14"
              type=""
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            ></el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      ref="tablelayout"
      class="commtable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        label="ID"
        prop="id"
        width="120"
        v-if="fundOption.includes('ID')"
      ></el-table-column>
      <el-table-column
        label="会员ID"
        prop="m_id"
        width="150"
        align="center"
        v-if="fundOption.includes('会员ID')"
      >
        <template slot-scope="scope">
          {{ scope.row.m_id || '-'}}
        </template>
      </el-table-column>
      <el-table-column
        v-if="fundOption.includes('用户名称')"
        label="用户名称"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.member.user_login || '-'}} </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="fundOption.includes('平台')"
        label="平台"
        prop="member.plate"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.member.plate || '-'}} </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="fundOption.includes('变更金额')"
        label="变更金额"
        prop="coin"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.coin || '-'}} </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="fundOption.includes('订单号')"
        label="订单号"
        prop="order"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.order || '-'}} </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="fundOption.includes('商品')"
        label="商品"
        align="center"
        min-width="160"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.msg || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="fundOption.includes('创建时间')"
        label="创建时间"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time)|| '-' }} </span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="created_at"
        label="操作"
        width="100"
        fixed="right"
        align="center"
      >
        <template slot-scope="scope">
          <div class="flex btnflex">
            <div class="commhover btn_green c_fff"
              @click="handleEdit(scope.row)"
            >
              <i class="el-icon-edit f-14"></i> 编辑
            </div>
          </div>
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.num"
      @pagination="getList"
    />

    <el-dialog
      :title="isadd ? '添加' : '编辑'"
      :visible.sync="dialogAddVisible"
      width="680px"
      class="dialogheaderbg"
      :before-close="handleCloseAdd"
      :close-on-click-modal='false'
    >
      <add-fund v-if="dialogAddVisible"
        ref="addmember"
        :isadd="isadd"
        :row="row"
        @close="handleCloseAdd"
      />
    </el-dialog>
  </div>
</template>
<script>
import { mapState } from "vuex";
import AddFund from "./AddFund.vue"
import { getOrderIndex, orderIndexExport} from '@/api/pay'
export default {
  data() {
    return {
      queryParams: {
        page: 1,
        num: 10,
        keyword: undefined,
      },
      total: 0,
      loading: false,
      tableData: [],
      checkedOption: [],
      isrefresh: false,
      selectoptions: [
        { id: 1, label: "ID" },
        { id: 2, label: "会员ID" },
        { id: 3, label: "用户名称" },
        { id: 4, label: "平台" },
        { id: 5, label: "变更金额" },
        { id: 6, label: "订单号" },
        { id: 7, label: "商品" },
        { id: 8, label: "创建时间" }
      ],
      dialogAddVisible: false,
      multiple: true,
      isadd: true,
      title: "",
      row: null,
      dialogVisible:false,
      form: {},
      rules:{
        level: [
          { required: true, message: "请选择会员等级", trigger: "change" },
        ],
        time: [{ required: true, message: "请选择有效期", trigger: "change" }]
      },
      dialogCzVisible:false,

      dialogQdVisible:false,
      exportloading:false
    };
  },

  props: {
    type: {
      default:'',
    },
  },

  components: {
    AddFund,
  },

  computed: {
    ...mapState({
      fundOption: (state) => state.paramsoption.fundOption,
    }),
  },

  created() {
    let fundOption = JSON.parse(localStorage.getItem("fundOption"));
    if (fundOption) {
      this.changeSelect(fundOption);
    }
    this.checkedOption = this.fundOption;
    this.getList();
  },

  methods: {
    getList() {
      this.loading = true;
      let params = {
        page: this.queryParams.page,
        num: this.queryParams.num
      }
      if(this.queryParams.keyword){
        params.keyword = this.queryParams.keyword
      }
      getOrderIndex(params).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.count;
        this.loading = false;
        this.isrefresh = false;
      });
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      this.isrefresh = true;
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSelectionChange(selection) {
      this.rowids = selection.map((item) => item.id);
      // this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    changeSelect(arr) {
      this.$store.commit("SET_FUNDOPTION", arr);
      this.$nextTick(() => {
        //在数据加载完，重新渲染表格
        this.$refs.tablelayout && this.$refs.tablelayout.doLayout();
      });
    },

    handleCommand(command) {
      console.log(command);
    },

    handleAdd() {
      this.isadd = true;
      this.dialogAddVisible = true;
    },

    handleCloseAdd(type) {
      if(type == 1){
        this.getList()
      }
      this.dialogAddVisible = false;
    },

    handleEdit(row) {
      this.row = row;
      this.isadd = false;
      this.dialogAddVisible = true;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.id || this.rowids;
      this.$modal.confirm('是否确认删除访问编号为"' + infoIds + '"的数据项？').then(function() {
        return delLogininfor(infoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    submit(){
      this.$refs.form.validate(valid=>{
        if(!valid) return

      })
    },

    handleClose() {
      this.dialogVisible = false;
    },

    handleCz(row) {
      this.dialogCzVisible = true;
    },

    handleCloseCz(type){
      if(type == 1){
        this.getList()
      }
      this.dialogCzVisible = false
    },

    handleExport(){
      let params = {
        head:['ID','会员id','用户名称','平台','变更金额','订单号','商品','创建时间'],
        keys:['id','member.id','member.user_login','member.plate','coin','order','msg','create_time'],
      }
      if(this.queryParams.keyword){
        params.keyword = this.queryParams.keyword
      }
      this.exportloading = true
      orderIndexExport(params).then(res=>{
        let url = res.data
        this.$message.success("正在下载，请稍后");
        var a = document.createElement("a");
        a.setAttribute("href", url);
        a.click();
        this.exportloading = false
      })
    }

  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 32px;
  border: 1px solid #eee;
  border-radius: 2px;
}
.imgmini {
  width: 40px;
  height: 40px;
  margin: auto;
}
.imgbox {
  width: 120px;
  height: 120px;
}
</style>
<style lang="scss">
.noheaderdia {
  .el-dialog__header {
    padding: 0 !important;
  }
  .el-dialog__body {
    padding: 0 !important;
  }
}
</style>